package com.jiecao.news.jiecaonews.util;

import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Rect;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;

public class ManagerUtil {
	/** 获取当前版本的代码级别 */
	public static Float getVerCode(Context context) {
		Float verCode = -1.0f;
		try {
			verCode = Float.valueOf(context.getPackageManager().getPackageInfo(
					context.getPackageName(), 0).versionCode);
		} catch (NameNotFoundException e) {
			Log.e("@@@", e.getMessage());
		}
		return verCode;
	}

	/** 获取当前版本的程序名称 */
	public static String getVerName(Context context) {
		String verName = "";
		try {
			verName = context.getPackageManager().getPackageInfo(
					context.getPackageName(), 0).versionName;
		} catch (NameNotFoundException e) {
			Log.e("@@@", e.getMessage());
		}
		return verName;
	}

	/**
	 * 用来判断服务是否运行.
	 * 
	 * @param context
	 * @param className
	 *            判断的服务名字：包名+类名
	 * @return true 在运行, false 不在运行
	 */

	public static boolean isServiceRunning(Context context, String className) {
		boolean isRunning = false;
		ActivityManager activityManager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<ActivityManager.RunningServiceInfo> serviceList = activityManager
				.getRunningServices(Integer.MAX_VALUE);
		if (!(serviceList.size() > 0)) {
			return false;
		}
		for (int i = 0; i < serviceList.size(); i++) {
			if (serviceList.get(i).service.getClassName().equals(className) == true) {
				isRunning = true;
				break;
			}
		}
		Log.i("@@@", "service is running?==" + isRunning);
		return isRunning;
	}

	/**
	 * 获取屏幕宽度
	 * 
	 * @param activity
	 * @return
	 */
	public static int getWidth(Activity activity) {
		DisplayMetrics dm = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm.widthPixels;
	}
	public static int getWidthByContext(Context context){
		DisplayMetrics dm = context.getResources().getDisplayMetrics();
		return dm.widthPixels;
	}

	/**
	 * 获取屏幕高度
	 * 
	 * @param activity
	 * @return
	 */
	public static int getHeight(Activity activity) {
		DisplayMetrics dm = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm.heightPixels;
	}

	/**
	 * 获取状态栏高度
	 * 
	 * @param activity
	 * @return
	 */

	public static int getStatusBarHeight(Activity activity) {
		Rect rect = new Rect();
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
		return rect.top; // 状态栏高度
	}

	/**
	 * 获取出去状态栏后的屏幕高度
	 */
	public static int getNoSatusBarHeight(Activity activity) {
		DisplayMetrics dm = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		Rect rect = new Rect();
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
		return dm.heightPixels - rect.top;
	}

	/**
	 * 获取网络状态
	 */
	public static boolean isNetConnect(Context context) {
		ConnectivityManager cwjManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cwjManager.getActiveNetworkInfo();
		if (info != null && info.isAvailable()) {
			// 能联网
			return true;
		} else {
			// 不能联网
			return false;
		}
	}

	/**
	 * 获取IMSI编号
	 * 
	 * @param context
	 * @return
	 */
	public static String getImei(Context context) {
		TelephonyManager mTelephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		return mTelephonyManager.getDeviceId();
	}
	/**
	 * 连接状态
	 * @param context
	 * @return
	 */
	public static int ConnectState(Context context){
		int connectType;
		ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetwork = manager.getActiveNetworkInfo();
		if((activeNetwork  == null) ||   
                (!activeNetwork.isConnectedOrConnecting())){
			connectType = 0;
			return connectType;
		}
		int type = activeNetwork.getType();
		if(ConnectivityManager.TYPE_WIFI == type){
			connectType = 1;
		}
//		else if(ConnectivityManager.TYPE_BLUETOOTH == type ){
//			connectType = 2;
//		}
		else if(ConnectivityManager.TYPE_MOBILE == type){
			connectType = 3;
		}else{
			System.out.println(activeNetwork.getTypeName());
			connectType = 0;
		}
		
		return connectType;
	}
	/*
	 * 检查是否存在sd卡
	 */
	public static boolean hasSdcard() {
		String state = Environment.getExternalStorageState();
		if (state.equals(Environment.MEDIA_MOUNTED)) {
			return true;
		} else {
			return false;
		}
	}
}
